package com.hcc.repository.gen.config;

import com.hcc.repository.core.constants.DbType;
import com.hcc.repository.core.utils.CollUtils;
import com.hcc.repository.gen.constants.TemplateEngineType;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * GlobalConfig
 *
 * @author hushengjun
 * @date 2023/9/16
 */
@Slf4j
@Data
public class GlobalConfig {

    private List<String> generateTables;
    private DbType dbType;

    private PackageConfig packageConfig;
    @Setter(AccessLevel.NONE)
    private TemplateConfig templateConfig = new TemplateConfig();
    private EntityConfig entityConfig = new EntityConfig();
    private MapperConfig mapperConfig = new MapperConfig();
    private RepositoryConfig repositoryConfig = new RepositoryConfig();
    private RepositoryImplConfig repositoryImplConfig = new RepositoryImplConfig();
    private ServiceConfig serviceConfig = new ServiceConfig();
    private ServiceImplConfig serviceImplConfig = new ServiceImplConfig();
    private ControllerConfig controllerConfig = new ControllerConfig();
    private JavaDocConfig javaDocConfig = new JavaDocConfig();

    private boolean enableGenerateEntity = true;
    private boolean enableGenerateMapper = true;
    private boolean enableGenerateRepository = true;
    private boolean enableGenerateRepositoryImpl = true;
    private boolean enableGenerateService = true;
    private boolean enableGenerateServiceImpl = true;
    private boolean enableGenerateController = true;

    public void setTemplateEngineType(TemplateEngineType templateEngineType) {
        if (templateEngineType == null) {
            throw new IllegalArgumentException("模板引擎类型不能为空");
        }
        templateConfig.setTemplateEngineType(templateEngineType);
    }

    public List<String> getGenerateTables() {
        if (CollUtils.isEmpty(generateTables)) {
            throw new IllegalArgumentException("必须设置需要生成代码的表名");
        }
        return generateTables;
    }

    public PackageConfig getPackageConfig() {
        if (packageConfig == null) {
            throw new IllegalArgumentException("必须设置软件包配置");
        }
        return packageConfig;
    }

    public boolean isEnableGenerateRepositoryImpl() {
        if (!enableGenerateRepository) {
            // repository开关未开启时，repositoryImpl也不生成
            log.debug("由于关闭了repository生成，repositoryImp也不生成");
            return false;
        }
        return enableGenerateRepositoryImpl;
    }

    public boolean isEnableGenerateServiceImpl() {
        if (!enableGenerateService) {
            // service开关未开启时，serviceImpl也不生成
            log.debug("由于关闭了service生成，serviceImp也不生成");
            return false;
        }
        return enableGenerateServiceImpl;
    }

}
